package com.lin.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.lin.service.BookServiceImpl;
import com.lin.service.IBookService;
import com.lin.utils.UUIDUtils;
import com.lin.vo.Book;
@WebServlet(name="AddBookServlet",urlPatterns={"/addBookServlet"})
public class AddBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IBookService bookService = new BookServiceImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		// 创建磁盘文件项工厂
		DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
		// 创建解析器工厂
		ServletFileUpload servletFileUpload =  new ServletFileUpload(diskFileItemFactory);
		servletFileUpload.setHeaderEncoding("UTF-8");
		// 解析request
		List<FileItem> fileItemLists;
		Map<String, String> map =new HashMap<>();
		Book book = new Book();
		String fieldName =null;
		try {
			fileItemLists = servletFileUpload.parseRequest(request);
			
			// 遍历集合得到每一个项
			for(FileItem fileItem : fileItemLists){
				if(fileItem.isFormField()){
					// 普通项目
					// 获取参数名称
					String name = fileItem.getFieldName();
					String value = fileItem.getString("UTF-8");
					/**
					 * name = value (存储map集合)
					 */
					map.put(name, value);
				}else{
					// 上传项
					fieldName = fileItem.getName();
					System.out.println(fieldName);
					// 获取目标文件的真实路径
					String realPath = this.getServletContext().getRealPath("/book_img");
					String path="E:\\workingspace\\estore\\WebContent\\book_img";
					// 获取输入流
					InputStream is = fileItem.getInputStream();
					// 获取输出流
					OutputStream os = new FileOutputStream(new File(realPath, fieldName));
				
					// 创建一个写磁盘的输出流
					OutputStream out = new FileOutputStream(new File(path, fieldName));
					int length = 0;
					byte [] buf = new byte[1024];
					while((length = is.read(buf))!=-1){
						os.write(buf, 0, length);
						out.write(buf, 0, length);
					}
					is.close();
					os.close();
					out.close();
				}
			}
			// 使用工具类封装数据
			BeanUtils.populate(book, map);
			// 手动补全其他字段
			book.setBid(UUIDUtils.getUUID());
			book.setImage("book_img/"+fieldName);
			book.setIsdel(0);
			// 调用业务层保存新书
			bookService.saveBook(book);
	
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.getRequestDispatcher("/adminBookServlet?method=findAllBook").forward(request, response);
	}

}